package com.mapeditor.core 
{
	import AS3Events.AS3EventMessage;
	import AS3Events.AS3EventRouter;
	import AS3Util.Tr;
	import com.mapeditor.graph.Menu;
	import com.mapeditor.math.Vector3;
	import com.mapeditor.util.GlobalContainer;
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuBuiltInItems;
	import flash.ui.ContextMenuItem;
	/**
	 * ...
	 * @author SE
	 */
	public class Item extends Sprite
	{
		public var id:String;
		public var type:uint;
		public var shape:Sprite;
		public var path:Sprite;
		public var _posOffset:Vector3;
		public var _startPos:Vector3;
		
		public var life:uint;
		public var frequence:Number;
		public var dist:uint;
		public var speed:uint;
		public var powerup_tiro:uint;
		public var powerup_life:uint;
		public var points:Array;
		public var pointsHead:Sprite;
		
		
		public var myContextMenu:ContextMenu;
		
		public static const PLAYER:uint = 1;
		public static const SHIP:uint = 2;
		public static const TOWER:uint = 3;
		public static const PLATFORM:uint = 4;
		public static const POWERUP:uint = 5;
		public static const PORTAL:uint = 6;
		public static const BRIDGE:uint = 7;
		public static const ROOM:uint = 8;
		public static const BUILD:uint = 9;
		
		
		public function Item(id:String,type:uint,shape:Sprite = null) 
		{
			this.id = id;
			this.type = type;
			this.path = new Sprite();
			this.pointsHead = new Sprite();
			this.shape = shape;
			addChild(path);
			addChild(shape);
			
			this.mouseChildren = false;
			this.mouseEnabled = true;
			
			_posOffset = new Vector3;
			
			this.life = 100;
			this.frequence = 0.5;
			this.dist = 1000;
			this.speed  = 200;
			this.powerup_tiro  = 1;
			this.powerup_life  = 0;
			this.points = new Array();
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, _startMove, false, 0, true);
			_createMenu();
			
			//head do path
			var pHead:Array = new Array();
            
            pHead.push(new Vector3(0,-1));
            pHead.push(new Vector3(8,10));
            pHead.push(new Vector3(-8,10));
            pHead.push(new Vector3(0,-1));
            
			
			pointsHead.graphics.beginFill(0x00FF00);
			pointsHead.graphics.moveTo(pHead[0].x,pHead[0].y);
			for (var i:uint = 1; i < 4;i++)
				pointsHead.graphics.lineTo(pHead[i].x, pHead[i].y);
			pointsHead.graphics.endFill();
			
			
		}
		private function setMenu():void 
		{
			var menu:Menu = Menu.getInstance();
			if (this.type == TOWER || this.type == SHIP || this.type == POWERUP)
			{
				menu.setValues(this);
				menu.x = this.x;
				menu.y = this.y;
				menu.show();
				
				drawPath();
			}
			else
			{
				menu.clear();
				this.path.graphics.clear();
			}
		}
		public function clearPath():void {
			this.path.graphics.clear();	
			if (contains(pointsHead))
				removeChild(pointsHead);
		}
		public function drawPath():void
		{
			this.path.graphics.clear();	
			if (points.length <= 1)
				return;
			this.path.graphics.clear();	
			
			
			this.path.graphics.lineStyle(1, 0xFFFF00);
			this.path.graphics.moveTo(points[0].x - this.x, points[0].y - this.y)
			
			for (var i:uint = 1, s:uint = points.length; i < s; i++)
				this.path.graphics.lineTo(points[i].x - this.x, points[i].y - this.y);				
			
			this.path.graphics.lineTo(points[0].x - this.x, points[0].y - this.y);
			
			var v:Vector3 = new Vector3();
			var u:Vector3 = new Vector3(0,-1,0);


			v.x = this.points[1].x - this.points[0].x;
			v.y = this.points[1].y - this.points[0].y;
			v.normalize();

			var angle:Number = u.angle(v);
			var angleG:Number = 180*angle/Math.PI;

			this.pointsHead.x = this.points[1].x-this.x;
			this.pointsHead.y = this.points[1].y-this.y;

			this.pointsHead.rotation = angleG;
			if (!contains(pointsHead))
				addChild(pointsHead);
		}
		
		private function moveMenu():void {
			var menu:Menu = Menu.getInstance();
			
			menu.x = this.x;
			menu.y = this.y;
		}
		
		private function _startMove(e:MouseEvent):void
		{
			var pf:PathFactory = PathFactory.getInstance();
			if (pf.active)
			{
				AS3EventRouter.SendEvent("class_item", AS3EventMessage.CreateMessage("item_mouse_down_pf_active", "", this));
				return;
			}	
			_posOffset.x = mouseX;
			_posOffset.y = mouseY;
			
			setMenu();
			
			
			_startPos = new Vector3(this.x, this.y);
			
			AS3EventRouter.SendEvent("class_item", AS3EventMessage.CreateMessage("item_mouse_down", "", this));
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, _move, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_UP, _up, false, 0, true);
			
			
		}
		
		
		private function _move(e:MouseEvent):void{
			
			if (this.x + (mouseX - _posOffset.x) > 0)
			{
				for (var i:uint = 0, s:uint = points.length; i < s; i++)
				{
					points[i].x += mouseX - _posOffset.x;
					points[i].x = GlobalContainer.checkPosGrid(points[i].x);					
				}
				this.x += mouseX - _posOffset.x;
				this.x = GlobalContainer.checkPosGrid(this.x);
				
				
			}
			
			if (this.y + (mouseY - _posOffset.y) > 0)
			{
				for (i= 0, s= points.length; i < s; i++)
				{
					points[i].y += mouseY - _posOffset.y
					points[i].y = GlobalContainer.checkPosGrid(points[i].y);
				}
				this.y += mouseY - _posOffset.y;
				this.y = GlobalContainer.checkPosGrid(this.y);
				
			}
			
			moveMenu();
			
			
		}//end of method
		
		
		private function _up(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,_move);
			stage.removeEventListener(MouseEvent.MOUSE_UP,_up);
			
			this.x = GlobalContainer.checkPosGrid(this.x);
	  		this.y = GlobalContainer.checkPosGrid(this.y);
			
			moveMenu();
			
			AS3EventRouter.SendEvent("class_item", AS3EventMessage.CreateMessage("item_mouse_up", "", this));
		}
		
		private function _createMenu():void
		{
			this.myContextMenu= new ContextMenu();
			  
			this.myContextMenu.hideBuiltInItems();

			var defaultItems:ContextMenuBuiltInItems = this.myContextMenu.builtInItems;

			defaultItems.forwardAndBack = false;    
			defaultItems.loop = false;
			defaultItems.play = false;
			defaultItems.print = false;
			defaultItems.quality = false;
			defaultItems.rewind = false;
			defaultItems.save = false;
			defaultItems.zoom = false;
			
			_addItemMenu("Remove", 1);
			_addItemMenu("Send to back", 2);
			
			this.contextMenu = this.myContextMenu;
			
		}
		private function _addItemMenu(title:String,value:uint):void 
		{
			var i:uint = this.myContextMenu.customItems.length;
			this.myContextMenu.customItems[i] = new ContextMenuItem(title, false, true);
			this.myContextMenu.customItems[i].enabled = true;
			ContextMenuItem(this.myContextMenu.customItems[i]).addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function s(e:Event):void { _contextMenuEvent(value); });
		}
		
		private function _contextMenuEvent(val:uint):void {
			if(val==2)
				AS3EventRouter.SendEvent("class_item", AS3EventMessage.CreateMessage("send_to_back", "", this));
			else
			{
				//Tr.ace("remove1");
				AS3EventRouter.SendEvent("class_item", AS3EventMessage.CreateMessage("delete", "", this));
			}
		}
		
	}

}